const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    changeAll:false,
    result: [],
    cartList:[],
    allPrice:0,
    cartCount:0
  },

  gotoShop(e){
    wx.navigateTo({
      url: '/pages/shop/shop?uid='+e.currentTarget.dataset.uid,
    })
  },

  onClickButton(){
    var that = this;
    console.log(this.data.result);
    console.log();
    if (this.data.result.length == 0) {
      return;
    }else{
      var shopList = [];
      for (let index = 0; index < this.data.result.length; index++) {
        for (let x = 0; x < this.data.cartList.length; x++) {
          if (this.data.result[index] == this.data.cartList[x].uid) {
            shopList.push(this.data.cartList[x]);
          }
          
        }
      }
      wx.navigateTo({
        url: '/pages/manyBuy/manyBuy?result=' + JSON.stringify(this.data.result) + '&shopList=' + JSON.stringify(shopList),
      });
    }
  },
  gotoIndex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  getCartList(){
    var that = this;
    wx.request({
      url: httpUrl + '/cart',
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        console.log(res);
        if (res.data.code == 20041) {
          that.setData({
            cartList:res.data.data.rows,
            cartCount:res.data.data.totalCount
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
        }else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },

  delete(e){
    var that = this;
    wx.request({
      url: httpUrl + '/cart',
      method:'DELETE',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      data:{
        shopId:e.currentTarget.dataset.uid
      },
      success(res){
        console.log(res);
        if (res.data.code == 20021) {
          that.getCartList();
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
        }else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },
  changeAll(){
    var changeAll = this.data.changeAll;
    if (changeAll) {
      this.setData({
        result:[],
        changeAll:false,
        checked:false,
        allPrice:0
      })
    }else{
      var newResult = [];
      for (let index = 0; index < this.data.cartList.length; index++) {
        newResult.push(this.data.cartList[index].uid.toString());
      }
      this.setData({
        result:newResult,
        changeAll:true,
        checked:true
      },()=>{
        var count = 0;
        for (let x = 0; x < this.data.result.length; x++) {
          var s = this.data.result[x];
          for (let j = 0; j < this.data.cartList.length; j++) {
            if (this.data.cartList[j].uid == s) {
              count += this.data.cartList[j].price * this.data.cartList[j].num;
            }
          }
        }
        this.setData({
          allPrice:count
        })
      })
    }
  },
  onChange(event) {
    this.setData({
      result: event.detail,
    },()=>{
      var count = 0;
      for (let index = 0; index < this.data.result.length; index++) {
        var s = this.data.result[index];
        for (let j = 0; j < this.data.cartList.length; j++) {
          if (this.data.cartList[j].uid == s) {
            count += this.data.cartList[j].price * this.data.cartList[j].num;
          }
        }
      }
      this.setData({
        allPrice:count
      })
    });
    
  },

  update(value){
    var i = value.currentTarget.dataset.index;
    var newList = this.data.cartList;
    newList[i].num = value.detail;
    this.setData({
      cartList:newList
    },()=>{
      var count = 0;
      for (let index = 0; index < this.data.result.length; index++) {
        var s = this.data.result[index];
        for (let j = 0; j < this.data.cartList.length; j++) {
          if (this.data.cartList[j].uid == s) {
            count += this.data.cartList[j].price * this.data.cartList[j].num;
          }
        }
      }
      this.setData({
        allPrice:count
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCartList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      result:[],
      allPrice:0,
      checked:false,
      changeAll:false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})